<template>
  <el-dialog title="选择人员" :visible="open" width="60%" @close="open = false">
    <el-row :gutter="24">
      <!--部门数据-->
      <el-col :span="6" :xs="24">
        <h6>部门列表</h6>
        <el-input v-model="filterText" placeholder="搜索" clearable style="margin-bottom: 10px;" />
        <div class="head-container">
          <el-tree
            :data="deptOptions"
            :props="defaultProps"
            :expand-on-click-node="false"
            :filter-node-method="filterNode"
            ref="tree"
            default-expand-all
            @node-click="handleNodeClick"
          />
        </div>
      </el-col>
      <el-col :span="10" :xs="24">
        <h6>待选人员</h6>
        <el-table
          ref="singleTable"
          :data="userList"
          border
          style="width: 100%"
          @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="用户名" align="center" prop="nickName" />
          <el-table-column label="部门" align="center" prop="dept.deptName" />
        </el-table>
        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
      <el-col :span="8" :xs="24">
        <h6>已选人员</h6>
        <el-tag v-for="(user,index) in userData" :key="index" closable  @close="handleClose(user)">
           {{user.nickName}} {{user.dept.deptName}}
        </el-tag>
      </el-col>
    </el-row>
    <span slot="footer" class="dialog-footer">
        <el-button @click="open = false">取 消</el-button>
        <el-button type="primary" @click="saveInfo">确 定</el-button>
      </span>
  </el-dialog>
</template>
<script>
import {deptTreeSelect,listUser} from "@/api/system/user";

export default {
  name:'UserSelect',
  data(){
    return {
      filterText:'',
      open: false,
      deptOptions: [],
      // 查询参数
      queryParams: {
        deptId: undefined
      },
      // 用户表格数据
      userList: [],
      defaultProps: {
        children: "children",
        label: "label"
      },
      userData:[],
      total: 0
    }
  },
  created(){
    this.getTreeselect();
  },
  methods:{
    /** 查询部门下拉树结构 */
    getTreeselect() {
      deptTreeSelect().then(response => {
        this.deptOptions = response.data;
      });
    },
    /** 查询用户列表 */
    getList() {
      listUser(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.userList = response.rows;
          this.total = response.total;
        }
      );
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.deptId = data.id;
      this.getList();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      if (selection) {
        this.userData = selection;
      }
    },
    // 关闭标签
    handleClose(tag) {
      this.userData.splice(this.userData.indexOf(tag), 1);
      this.$refs.singleTable.toggleRowSelection(tag, false)
    },
    saveInfo(){
      this.open = false;
      this.$emit('saveUsers', this.userData)
    }
  },
  watch:{
    filterText(newVal){
      this.$refs.tree.filter(newVal);
    }
  }
}
</script>